package abq2.jic.jic.plugin.operate.lostandfound;

import kd.bos.form.gpt.IGPTAction;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class GetNewUrl implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String , String> result = new HashMap<>();
        if ("GET_NEW_URL".equalsIgnoreCase(action)) {
            String urlResult = params.get("urlResult");

            //获取当前时间，我们可以在isv文件夹中根据时间来对相应的文件夹创建HTML文件
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String directoryName = simpleDateFormat.format(new Date());
            //获取一个10位的随机文件名称
            StringBuffer sb = new StringBuffer();
            for (int i = 1 ; i<=10; i++) {
                int ascii = 48+(int)(Math.random()*9);
                char c = (char) ascii;
                sb.append(c);
            }
            File directoryGetQing = new File(System.getProperty("JETTY_WEBRES_PATH")+"/isv/gptQing",directoryName);
            //如果文件夹不存在就创建文件夹
            if (!directoryGetQing.exists()) {
                directoryGetQing.mkdirs();
            }

            File targetFile = new File(System.getProperty("JETTY_WEBRES_PATH")+"/isv/gptQing/"+directoryName,sb+".jpg");
            if (!targetFile.exists()) {
                try {
                    targetFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                //写入文件
                File image = new File(urlResult);
                fis = new FileInputStream(image);
                byte[] bytes = new byte[1024];//1kb

                fos=new FileOutputStream(targetFile);
                int len;//记录每次读入到buffer中的字节的个数
                while((len = fis.read(bytes)) != -1)
                {
                    fos.write(bytes,0,len);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                //4.关闭资源
                try
                {
                    if(fis != null)
                    {
                        fis.close();
                    }
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
                try
                {
                    if(fos != null)
                    {
                        fos.close();
                    }
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            //返回URL，其中gaiIframeSize={"height":380,"width":1000} 用于调整展示窗口宽高
            result.put("resultHtmlUrl" , System.getProperty("domain.contextUrl")+"/isv/gptQing/"+directoryName+"/"+sb+".jpg?");
        }
        return result;
    }
}
